﻿Imports System.Data.OleDb
Imports System.Data

Partial Class ProfileModify
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("myFirstDatabase").ConnectionString)
        Dim SqlString As String = "SELECT * FROM tblProfile where Neighbor=@f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        oleDbConn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        If dr.HasRows() Then
            dr.Read()

            If Not IsDBNull(dr("FirstName")) Then tbFirstName.Text = dr("FirstName")
            If Not IsDBNull(dr("LastName")) Then tbLastName.Text = dr("LastName")
            If Not IsDBNull(dr("Alias")) Then tbAlias.Text = dr("Alias")

        End If

    End Sub

    Protected Sub btnProfile_Click(sender As Object, e As System.EventArgs) Handles btnProfile.Click
        Dim myGUID As System.Guid
        myGUID = Guid.NewGuid()
        Dim newFileName = myGUID.ToString() + ".jpg"

        If ProfilePicUl.HasFile Then
            Dim picLocationOnServerHardDisk = Request.MapPath("pix") & "/" & newFileName
            ProfilePicUl.SaveAs(picLocationOnServerHardDisk)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("myFirstDatabase").ConnectionString)
        'Dim SqlString As String = "UPDATE tblprofile SET firstname=@f1,lastname=@f2,alias=@f3, avatar=@f4, submitteddate=@f6 WHERE Neighbor=@f5"
        Dim SqlString As String = "Insert into tblprofile(firstname,lastname,alias,avatar,neighbor,submitteddate) Values (@f1,@f2,@f3,@f4,@f5,@f6)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        Dim SubmittedDate As DateTime = Now()
        Dim format As String = "ddd MMM yyyy"
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tbFirstName.Text)
        cmd.Parameters.AddWithValue("@f2", tbLastName.Text)
        cmd.Parameters.AddWithValue("@f3", tbAlias.Text)
        cmd.Parameters.AddWithValue("@f4", "pic/" & newFileName)
        cmd.Parameters.AddWithValue("@f5", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f6", SubmittedDate.ToString(format))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("Profile.aspx")
    End Sub
End Class
